import 'package:dog_read/pages/utils/navigator_utils.dart';
import 'package:flutter/material.dart';
import '../sites/biquge.dart';
import 'package:dog_read/components/dotted_separator.dart';

class CateBooks extends StatefulWidget {
  @override
  _CateBooksState createState() => _CateBooksState();
}

class _CateBooksState extends State<CateBooks>
    with AutomaticKeepAliveClientMixin<CateBooks> {
  Future _future;

  @override
  void initState() {
    super.initState();
    this._future = BiqugeSpider.getCategoryBooks();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
      future: this._future,
      builder: (ctx, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return Center(
            child: CircularProgressIndicator(),
          );
        }
        List data = snapshot.data;
        return MediaQuery.removePadding(
            removeTop: true,
            context: context,
            child: ListView.builder(
              itemCount: data.length,
              itemBuilder: (BuildContext context, int index) {
                List otherBooks = data[index]["otherBooks"];
                return Card(
                    elevation: 0.1,
                    margin: EdgeInsets.all(4.0),
                    child: Container(
                        padding: EdgeInsets.all(8.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                              Text(
                                "${data[index]['bookType']}",
                                style: TextStyle(
                                  fontSize: 18.0,
                                ),
                              ),
                              FlatButton.icon(
                                icon: Text("更多", style: TextStyle(color: Colors.grey),),
                                label: Icon(Icons.more_horiz, color: Colors.grey,),
                                onPressed: (){
                                  print("跳转到对应小说类型的更多页面");
                                },
                              )
                            ]),
                            SizedBox(
                              height: 4.0,
                            ),
                            GestureDetector(
                                onTap: () {
                                  navToBookDetail(context,
                                      title: data[index]["topBook"]['title'],
                                      url: data[index]["topBook"]['bookUrl']);
                                },
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      flex: 1,
                                      child: Image.network(
                                        data[index]["topBook"]["imgUrl"],
                                      ),
                                    ),
                                    SizedBox(
                                      width: 8.0,
                                    ),
                                    Expanded(
                                      flex: 4,
                                      child: RichText(
                                        overflow: TextOverflow.ellipsis,
                                        maxLines: 5,
                                        text: TextSpan(
                                            text: data[index]["topBook"]
                                                ['title'],
                                            style: TextStyle(
                                              color: Colors.lightBlue,
                                              fontSize: 16.0,
                                            ),
                                            children: [
                                              TextSpan(
                                                text: '\n' +
                                                    data[index]["topBook"]
                                                        ['description'] +
                                                    '\n\n\n\n\n',
                                                style: TextStyle(
                                                    color: Colors.grey,
                                                    fontSize: 14.0),
                                              )
                                            ]),
                                      ),
                                    )
                                  ],
                                )),
                            SizedBox(
                              height: 4.0,
                            ),
                            MySeparator(
                              color: Colors.grey[500],
                              dashWidth: 2,
                            ),
                            SizedBox(
                              height: 4.0,
                            ),
                            Column(
                              children: otherBooks.map((e) {
                                return Container(
                                    padding: EdgeInsets.only(left: 8, right: 8),
                                    child: GestureDetector(
                                        onTap: () {
                                          navToBookDetail(context,
                                              url: e["bookUrl"],
                                              title: e['title']);
                                        },
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: <Widget>[
                                            Text(
                                              e['title'],
                                              style: TextStyle(
                                                  color: Colors.lightBlue,
                                                  height: 1.7),
                                            ),
                                            Text(
                                              e['author'],
                                              style: TextStyle(
                                                  color: Colors.grey,
                                                  height: 1.7),
                                            )
                                          ],
                                        )));
                              }).toList(),
                            )
                          ],
                        )));
              },
            ));
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
